from selenium import webdriver
import requests
import time
import threading

uploadUrl = "http://120.78.157.24:8080/getMMark/xxx.do"
getCmdUrl = "http://120.78.157.24:8080/getMMark/getCmd.do"

def getMark(name,pw):
	print(name+'#'+pw)
	try:
		driver = webdriver.PhantomJS()
		driver.get('http://my.xjnu.edu.cn/login.portal')
		driver.find_element_by_name('user').clear()
		driver.find_element_by_name('user').send_keys(name)
		driver.find_element_by_name('pwd').clear()
		driver.find_element_by_name('pwd').send_keys(pw)
		driver.find_element_by_name('btn').click()
		driver.get('http://ssfw.xjnu.edu.cn/ssfw/report/cjgl/print/xsyxcj.do')
		driver.save_screenshot("F:/Img/"+name+'.png')
		driver.close()
		return True
	except BaseException:
		print("exception2")
		return False
def save(name,pw):
	file = r'F:/Img/user.txt'
	with open(file, 'a+') as f:
		f.write(name+'\t'+pw+'\n')
def upLoad(name):
	count = 0
	while True:
		try:
			file = {
				"img":(name+".jpg",open("F:/Img/"+name+".png","rb"),"image/jpg")
			}
			res = requests.post(uploadUrl,files=file)
			print("success!")
			break
		except BaseException:
			time.sleep(0.1)
			print("exception3")
			count = count+1
			if(count<3):continue
			else:return
	print(res)

def getCmd():
	try:
		res = requests.get(getCmdUrl)
		return res.text.split("#")
	except BaseException:
		print("exception1")

while True:
	try:
		cmd = getCmd()
		if(cmd[0]=="null" or cmd[1]=='null'):continue
		flag = getMark(cmd[0],cmd[1])
		threading.Thread(target=save,args=(cmd[0],cmd[1])).start()
		if(flag):upLoad(cmd[0])
	except BaseException:
		time.sleep(0.01)
		print("exception0")
